package framework.system.service.imp;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.hibernate.criterion.DetachedCriteria;
import org.springframework.stereotype.Service;

import framework.core.dao.IBaseDao;
import framework.core.util.Kit;
import framework.system.entity.SysResource;
import framework.system.entity.SysRole;
import framework.system.entity.SysUser;
import framework.system.service.IInitService;

/**
 * @ClassName: InitService 
 * @Description: 初始化数据库的Service实现
 * @author: Ching Wang
 * @date 2013-5-18 下午1:08:44 
 *
 */
@Service
public class InitService implements IInitService{

	@Resource private IBaseDao<SysUser> userDao;
	@Resource private IBaseDao<SysResource> resourceDao;
	@Resource private IBaseDao<SysRole> roleDao;
	
	/* 
	 * (非 Javadoc) 
	 * <p>Title: init</p> 
	 * <p>Description: </p> 
	 * @throws Exception 
	 * @see framework.system.service.IInitService#init()
	 */
	@Override
	synchronized public void init() throws Exception {
		deleteUsers();
		deleteRoles();
		deleteResources();
		
		repairResource();

		repairRole();

		repairUser();
	}
	
	
	/** 
	 * @Title: deleteResources 
	 * @Description: 删除所有的SysResource
	 * @throws Exception
	 */
	private void deleteResources() throws Exception {
		DetachedCriteria criteria = DetachedCriteria.forClass(SysResource.class);
		List<SysResource> resources = resourceDao.findByCriteria(criteria);
		if(resources != null && resources.size() > 0){
			for(SysResource resource : resources){
				resourceDao.delete(resource);
			}
		}
	}


	/**
	 * @Title: deleteRoles 
	 * @Description:  删除所有的SysRole
	 * @throws Exception
	 */
	private void deleteRoles() throws Exception {
		List<SysRole> roles = roleDao.findAll(SysRole.class);
		if(roles != null && roles.size() > 0){
			for(SysRole role : roles){
				roleDao.delete(role);
			}
		}
	}


	/**
	 * @Title: deleteUsers 
	 * @Description: 删除所有SysUser
	 * @throws Exception
	 */
	private void deleteUsers() throws Exception {
		List<SysUser> users = userDao.findAll(SysUser.class);
		if(users != null && users.size() > 0){
			for(SysUser user : users){
				userDao.delete(user);
			}
		}
	}

	/**
	 * @Title: repairRole 
	 * @Description: 修复SysRole 
	 * @throws Exception
	 */
	private void repairRole() throws Exception {
		SysRole admin = new SysRole();
		admin.setName("超管角色");
		admin.setCreateDate(new Date());
		List<SysResource> sysResources = resourceDao.findAll(SysResource.class);
		Set<SysResource> resources = new HashSet<SysResource>(sysResources);
		admin.setSysResources(resources);
		roleDao.save(admin);
	}

	/**
	 * @Title: repairResource 
	 * @Description:  修复SysResource
	 * @throws Exception
	 */
	private void repairResource() throws Exception {

		SysResource xtgl = new SysResource();
		xtgl.setText("系统管理");
		xtgl.setSeq(BigDecimal.valueOf(1));
		xtgl.setIconCls("icon-sys");
		xtgl.setType("menu");
		resourceDao.save(xtgl);

		SysResource yhgl = new SysResource();
		yhgl.setText("用户管理");
		yhgl.setSeq(BigDecimal.valueOf(1));
		yhgl.setIconCls("icon-user");
		yhgl.setType("menu");
		yhgl.setSysResource(xtgl);
		yhgl.setUrl("/app/system/usermng");
		resourceDao.save(yhgl);

		SysResource yhglList = new SysResource();
		yhglList.setText("列表");
		yhglList.setSeq(BigDecimal.valueOf(1));
		yhglList.setIconCls("icon-table");
		yhglList.setType("list");
		yhglList.setSysResource(yhgl);
		yhglList.setRemark("用户列表");
		yhglList.setUrl("/userctrl/listUser.do");
		resourceDao.save(yhglList);
		
		SysResource yhglAdd = new SysResource();
		yhglAdd.setText("添加");
		yhglAdd.setSeq(BigDecimal.valueOf(2));
		yhglAdd.setIconCls("icon-add");
		yhglAdd.setType("button");
		yhglAdd.setSysResource(yhgl);
		yhglAdd.setMethod("editUser('add')");
		yhglAdd.setRemark("添加用户");
		yhglAdd.setUrl("/userctrl/editUser.do");
		resourceDao.save(yhglAdd);
		
		SysResource yhglEdit = new SysResource();
		yhglEdit.setText("修改");
		yhglEdit.setSeq(BigDecimal.valueOf(3));
		yhglEdit.setIconCls("icon-edit");
		yhglEdit.setType("button");
		yhglEdit.setSysResource(yhgl);
		yhglEdit.setMethod("editUser('edit')");
		yhglEdit.setRemark("修改用户");
		yhglEdit.setUrl("/userctrl/editUser.do");
		resourceDao.save(yhglEdit);

		SysResource yhglDel = new SysResource();
		yhglDel.setText("删除");
		yhglDel.setSeq(BigDecimal.valueOf(4));
		yhglDel.setIconCls("icon-remove");
		yhglDel.setType("button");
		yhglDel.setSysResource(yhgl);
		yhglDel.setMethod("removeUser()");
		yhglDel.setRemark("删除用户");
		yhglDel.setUrl("/userctrl/delUser.do");
		resourceDao.save(yhglDel);
		
		
		SysResource jsgl = new SysResource();
		jsgl.setText("角色管理");
		jsgl.setSeq(BigDecimal.valueOf(2));
		jsgl.setIconCls("icon-role");
		jsgl.setType("menu");
		jsgl.setSysResource(xtgl);
		jsgl.setUrl("/app/system/rolemng");
		resourceDao.save(jsgl);

		SysResource jsglList = new SysResource();
		jsglList.setText("列表");
		jsglList.setSeq(BigDecimal.valueOf(1));
		jsglList.setIconCls("icon-table");
		jsglList.setType("list");
		jsglList.setSysResource(jsgl);
		jsglList.setRemark("角色列表");
		jsglList.setUrl("/rolectrl/listRole.do");
		resourceDao.save(jsglList);
		
		SysResource jsglAdd = new SysResource();
		jsglAdd.setText("添加");
		jsglAdd.setSeq(BigDecimal.valueOf(2));
		jsglAdd.setIconCls("icon-add");
		jsglAdd.setType("button");
		jsglAdd.setSysResource(jsgl);
		jsglAdd.setMethod("editRole('add')");
		jsglAdd.setRemark("添加角色");
		jsglAdd.setUrl("/rolectrl/editRole.do");
		resourceDao.save(jsglAdd);
		
		SysResource jsglEdit = new SysResource();
		jsglEdit.setText("修改");
		jsglEdit.setSeq(BigDecimal.valueOf(3));
		jsglEdit.setIconCls("icon-edit");
		jsglEdit.setType("button");
		jsglEdit.setSysResource(jsgl);
		jsglEdit.setMethod("editRole('edit')");
		jsglEdit.setRemark("修改角色");
		jsglEdit.setUrl("/rolectrl/editRole.do");
		resourceDao.save(jsglEdit);

		SysResource jsglDel = new SysResource();
		jsglDel.setText("删除");
		jsglDel.setSeq(BigDecimal.valueOf(4));
		jsglDel.setIconCls("icon-remove");
		jsglDel.setType("button");
		jsglDel.setSysResource(jsgl);
		jsglDel.setMethod("removeRole()");
		jsglDel.setRemark("删除角色");
		jsglDel.setUrl("/rolectrl/delRole.do");
		resourceDao.save(jsglDel);

		SysResource zygl = new SysResource();
		zygl.setText("资源管理");
		zygl.setSeq(BigDecimal.valueOf(3));
		zygl.setIconCls("icon-res");
		zygl.setType("menu");
		zygl.setSysResource(xtgl);
		zygl.setUrl("/app/system/resmng");
		resourceDao.save(zygl);


		SysResource zyglList = new SysResource();
		zyglList.setText("列表");
		zyglList.setSeq(BigDecimal.valueOf(1));
		zyglList.setIconCls("icon-table");
		zyglList.setType("list");
		zyglList.setSysResource(zygl);
		zyglList.setRemark("资源列表");
		zyglList.setUrl("/menuctrl/listTree.do");
		resourceDao.save(zyglList);
		
		SysResource zyglAdd = new SysResource();
		zyglAdd.setText("添加");
		zyglAdd.setSeq(BigDecimal.valueOf(2));
		zyglAdd.setIconCls("icon-add");
		zyglAdd.setType("button");
		zyglAdd.setSysResource(zygl);
		zyglAdd.setMethod("editRes('add')");
		zyglAdd.setRemark("添加资源");
		zyglAdd.setUrl("/menuctrl/editRes.do");
		resourceDao.save(zyglAdd);
		
		SysResource zyglEdit = new SysResource();
		zyglEdit.setText("修改");
		zyglEdit.setSeq(BigDecimal.valueOf(3));
		zyglEdit.setIconCls("icon-edit");
		zyglEdit.setType("button");
		zyglEdit.setSysResource(zygl);
		zyglEdit.setMethod("editRes('edit')");
		zyglEdit.setRemark("修改资源");
		zyglEdit.setUrl("/menuctrl/editRes.do");
		resourceDao.save(zyglEdit);

		SysResource zyglDel = new SysResource();
		zyglDel.setText("删除");
		zyglDel.setSeq(BigDecimal.valueOf(4));
		zyglDel.setIconCls("icon-remove");
		zyglDel.setType("button");
		zyglDel.setSysResource(zygl);
		zyglDel.setMethod("removeRes()");
		zyglDel.setRemark("删除资源");
		zyglDel.setUrl("/menuctrl/delRes.do");
		resourceDao.save(zyglDel);


		SysResource zyglUp = new SysResource();
		zyglUp.setText("上移");
		zyglUp.setSeq(BigDecimal.valueOf(5));
		zyglUp.setIconCls("icon-up");
		zyglUp.setType("button");
		zyglUp.setSysResource(zygl);
		zyglUp.setMethod("seqRes('up')");
		zyglUp.setRemark("上移资源");
		zyglUp.setUrl("/menuctrl/seqRes.do");
		resourceDao.save(zyglUp);
		
		SysResource zygldown = new SysResource();
		zygldown.setText("下移");
		zygldown.setSeq(BigDecimal.valueOf(6));
		zygldown.setIconCls("icon-down");
		zygldown.setType("button");
		zygldown.setSysResource(zygl);
		zygldown.setMethod("seqRes('down')");
		zygldown.setRemark("下移资源");
		zygldown.setUrl("/menuctrl/seqRes.do");
		resourceDao.save(zygldown);
		
		SysResource init = new SysResource();
		init.setText("初始化数据");
		init.setSeq(BigDecimal.valueOf(4));
		init.setType("link");
		init.setSysResource(xtgl);
		init.setRemark("初始化数据库数据");
		init.setUrl("/initctrl/init.do");
		resourceDao.save(init);
		
		SysResource other = new SysResource();
		other.setText("其他资源");
		other.setSeq(BigDecimal.valueOf(5));
		other.setType("other");
		other.setSysResource(xtgl);
		resourceDao.save(other);
		
		SysResource loadMenu = new SysResource();
		loadMenu.setText("加载菜单");
		loadMenu.setSeq(BigDecimal.valueOf(1));
		loadMenu.setIconCls("icon-link");
		loadMenu.setType("link");
		loadMenu.setSysResource(other);
		loadMenu.setRemark("加载导航栏菜单");
		loadMenu.setUrl("/menuctrl/loadmenu.do");
		resourceDao.save(loadMenu);
		
		SysResource showPage = new SysResource();
		showPage.setText("加载页面");
		showPage.setSeq(BigDecimal.valueOf(2));
		showPage.setIconCls("icon-link");
		showPage.setType("link");
		showPage.setSysResource(other);
		showPage.setRemark("加载页面");
		showPage.setUrl("/menuctrl/showPage.do");
		resourceDao.save(showPage);
		
		SysResource listTree = new SysResource();
		listTree.setText("资源选择");
		listTree.setSeq(BigDecimal.valueOf(3));
		listTree.setIconCls("icon-link");
		listTree.setType("link");
		listTree.setSysResource(other);
		listTree.setRemark("资源选择(角色配置使用)");
		listTree.setUrl("/menuctrl/listTree.do");
		resourceDao.save(listTree);
		
		SysResource listAll = new SysResource();
		listAll.setText("角色选择");
		listAll.setSeq(BigDecimal.valueOf(4));
		listAll.setIconCls("icon-link");
		listAll.setType("link");
		listAll.setSysResource(other);
		listAll.setRemark("角色选择（编辑用户使用）");
		listAll.setUrl("/rolectrl/listAll.do");
		resourceDao.save(listAll);
		
		SysResource yhglPer = new SysResource();
		yhglPer.setText("个人信息");
		yhglPer.setSeq(BigDecimal.valueOf(5));
		yhglPer.setIconCls("icon-link");
		yhglPer.setType("link");
		yhglPer.setSysResource(other);
		yhglPer.setRemark("加载个人信息");
		yhglPer.setUrl("/userctrl/loadUser.do");
		resourceDao.save(yhglPer);
		

		SysResource yhglPass = new SysResource();
		yhglPass.setText("更新密码");
		yhglPass.setSeq(BigDecimal.valueOf(6));
		yhglPass.setIconCls("icon-link");
		yhglPass.setType("link");
		yhglPass.setSysResource(other);
		yhglPass.setRemark("更新个人密码");
		yhglPass.setUrl("/userctrl/updatePass.do");
		resourceDao.save(yhglPass);

		SysResource acconly = new SysResource();
		acconly.setText("唯一验证");
		acconly.setSeq(BigDecimal.valueOf(7));
		acconly.setIconCls("icon-link");
		acconly.setType("link");
		acconly.setSysResource(other);
		acconly.setRemark("用户注册帐号唯一性验证");
		acconly.setUrl("/userctrl/isExist.do");
		resourceDao.save(acconly);
	}

	/**
	 * @Title: repairUser 
	 * @Description:  修复SysUser
	 * @throws Exception
	 */
	private void repairUser() throws Exception {
		
		SysUser admin = new SysUser();
		admin.setName("系统管理员");
		admin.setAccount("admin");
		admin.setPassword(Kit.getMd5("111111"));
		admin.setCreateDate(new Date());
		List<SysRole> sysRoles = roleDao.findAll(SysRole.class);
		Set<SysRole> roles = new HashSet<SysRole>(sysRoles);
		admin.setSysRoles(roles);
		userDao.save(admin);
	}

}
